package com.dgut.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dgut.entity.Deliverynotes;
import com.dgut.result.PageResult;
import com.dgut.result.Result;
import com.dgut.service.DeliverynotesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(tags = "发货单管理接口")
@RequestMapping("/deliverynotes")
@Slf4j
public class DeliverynotesController {
    @Autowired
    private DeliverynotesService deliverynotesService;

    /**
     * 生成发货单
     *
     * @param deliverynotes
     * @return
     */
    @PostMapping
    @ApiOperation("发货单添加")
    public Result add(@RequestBody Deliverynotes deliverynotes) {
        log.info("发货单添加：{}", deliverynotes);
        //调用service层
        deliverynotesService.add(deliverynotes);
        return Result.success();
    }

    /**
     * 发货单查询
     *
     * @param currentPage
     * @param pageSize
     * @return
     */
    @GetMapping
    @ApiOperation("发货单查询")
    public Result list(Integer currentPage, Integer pageSize) {
        log.info("发货单查询");
        // 分页查询 按创建时间降序
        QueryWrapper<Deliverynotes> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        Page<Deliverynotes> page = deliverynotesService.page(new Page<>(currentPage, pageSize), queryWrapper);
        return Result.success(new PageResult(page.getTotal(), page.getRecords()));
    }

    @PutMapping
    @ApiOperation("发货单修改")
    public Result update(@RequestBody Deliverynotes deliverynotes) {
        log.info("修改发货单: 参数deliverynotes:{}", deliverynotes);
        String response = deliverynotesService.update(deliverynotes);
        return Result.success(response);
    }
}
